< previous page page_325 next page >

Page 325
call it from Visual Basic.1 Fortunately, almost all DLL functions are exported with this calling convention.
In the stdcall calling convention, the following rules apply:
Parameters are passed onto the stack from right to left.
The called function pops parameters from the stack.
Why are parameters pushed onto the stack from right to left? Consider the following function:
Function MyFunction(Byval A as Long, ByVal B As Long) As Long
Now look at Figure T4-5, which shows the stack frame for the function when it is called with the line:
Call MyFunction ( 2, 1)
The leftmost parameter is pushed onto the stack last, which places it closest to the BP in the stack frame for the function. Why should this matter? The truth is that it doesn't. With some calling conventions, passing parameters from right to left makes it easier to handle situations where you have a variable number of parameters, but that doesn't apply in this case. The stdcall calling convention uses another approach, which involves passing a pointer to an array. As a Visual Basic programmer, you won't need to worry about the order.
0325-01.gif
Figure T4-5:
Stack frame for a function with two parameters
When Things Go Wrong
If the calling function pushes parameters onto the stack and the called function pops them from the stack, the two functions must agree on both the
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
1 Or can you? As you work your way through the puzzles, you may find a surprise or two on this score.

 
< previous page page_325 next page >